﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using IniFiles.Net;
using System.IO;

namespace IniExampleApp
{
  public partial class Form1 : Form
  {
    BaseIni Ini;
    enum IniTypes { TMemIniFile, TApiIniFile, TIniFile, TStreamIniFile };
    int Type = 0;

    public Form1()
    {
      InitializeComponent();

    }


    /// <summary>
    /// All basic ini handling for demonstration
    /// </summary> 
    private void doIniActions()
    {
      btLoad.Enabled = true;
      textBox1.Clear();
      Ini.Clear();
      log("Cleared ini. Using: " + Ini.FileName);
      
      //writing all types of values
      Ini.WriteFloat("Main", "Pi", Math.PI);
      log("Did: Ini.WriteFloat(\"Main\", \"Pi\", Math.PI)");
      Ini.WriteDate("Main", "Now", DateTime.Now);
      log("Did: Ini.WriteDate(\"Main\", \"Now\", DateTime.Now)");
   
      //write int values
      Ini.WriteInteger("Form location", "Top", this.Top);
      Ini.WriteInteger("Form location", "Left", this.Left);
      Ini.WriteInteger("Form location", "Width", this.Width);
      Ini.WriteInteger("Form location", "Height", this.Height);
      log("Wrote form information to the ini");

      Ini.WriteBool("Main", "Boolean", true);
      log("Did: Ini.WriteBool(\"Main\", \"Boolean\", true)");
    }
    #region Button actions


    private void btMemIniFile_Click(object sender, EventArgs e)
    {
      Ini = new TMemIniFile(Path.ChangeExtension(Application.ExecutablePath, ".ini"));
      Type = (int)IniTypes.TMemIniFile;
      doIniActions();
      ((TMemIniFile)Ini).UpdateFile();
    }


    private void btApiIniFile_Click(object sender, EventArgs e)
    {
      Ini = new TApiIniFile(Path.ChangeExtension(Application.ExecutablePath, ".ini"));
      Type = (int)IniTypes.TApiIniFile;
      doIniActions();
    }

    private void btIniFile_Click(object sender, EventArgs e)
    {      
      Ini = new TIniFile(Path.ChangeExtension(Application.ExecutablePath, ".ini"));
      Type = (int)IniTypes.TIniFile;
      doIniActions();
    }

    private void btLoad_Click(object sender, EventArgs e)
    {
      if (Type == (int)IniTypes.TMemIniFile) ((TMemIniFile)Ini).UpdateFile();
    
      textBox1.Text = File.ReadAllText(Ini.FileName);
    }

    #endregion

    #region Support functions

    private void log(string s)
    {
      textBox1.AppendText(s + "\r\n");
    }

    #endregion

   



  }
}